package com.example.emojibase.view;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.emojibase.AuthApiService;
import com.example.emojibase.R;
import com.example.emojibase.RetrofitClient;
import com.example.emojibase.pojo.Reply;
import com.example.emojibase.pojo.User;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Register extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        AuthApiService service = RetrofitClient.getClient().create(AuthApiService.class);
        EditText confirmPassword=findViewById(R.id.confirmPassword);
        EditText password=findViewById(R.id.password);
        EditText emailAddress=findViewById(R.id.emailAddress);
        EditText validation=findViewById(R.id.validation);
        Button commitButton=findViewById(R.id.button1);
        Button validationSendButton=findViewById(R.id.validationSendButton);
        validationSendButton.setOnClickListener(view -> {
            String email = emailAddress.getText().toString();
            service.sendValidation(email).enqueue(new Callback<Reply>() {
                @Override
                public void onResponse(@NonNull Call<Reply> call, @NonNull Response<Reply> response) {
                    if (response.isSuccessful()) {
                        Reply reply = response.body();
                        // 处理成功的响应
                        assert reply != null;
                        if (reply.getStatus()==1)
                            Toast.makeText(Register.this, "验证码已发送", Toast.LENGTH_SHORT).show();
                    } else {
                        // 处理错误响应
                        Toast.makeText(Register.this, "Response failed", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(@NonNull Call<Reply> call, @NonNull Throwable t) {
                    // 处理请求失败
                    Toast.makeText(Register.this, "Get response failed", Toast.LENGTH_SHORT).show();
                }
            });
        });
        commitButton.setOnClickListener(view -> {
            if (password.getText().toString().equals(confirmPassword.getText().toString())){
                User user=new User(emailAddress.getText().toString(),password.getText().toString());
                service.register(user,validation.getText().toString()).enqueue(new Callback<Reply>() {
                    @Override
                    public void onResponse(@NonNull Call<Reply> call, @NonNull Response<Reply> response) {
                        // 处理成功的响应
                        if (response.isSuccessful()) {
                            Reply reply=response.body();
                            assert reply != null;
                            if (reply.getStatus()==1) {
                                Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                            else
                                Toast.makeText(Register.this, reply.getMessage(), Toast.LENGTH_SHORT).show();
                        } else {
                            // 处理错误响应
                            Toast.makeText(Register.this, "Response failed", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(@NonNull Call<Reply> call, @NonNull Throwable t) {
                        // 处理请求失败
                        Toast.makeText(Register.this, "Get response failed", Toast.LENGTH_SHORT).show();
                    }
                });
            }
            else
                Toast.makeText(Register.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
        });

    }
}
